Skip to content

Structs with no fields or all-ZST fields are ZSTs#2262

Open
DanielEScherzer wants to merge 2 commits intorust-lang:masterfrom
DanielEScherzer:zst-structs
Open

Structs with no fields or all-ZST fields are ZSTs#2262
DanielEScherzer wants to merge 2 commits intorust-lang:masterfrom
DanielEScherzer:zst-structs

Conversation

@DanielEScherzer
Copy link
Copy Markdown
Contributor

@DanielEScherzer DanielEScherzer commented May 5, 2026

Guarantee that structs with Rust representation and with no fields, or where all fields are ZSTs, are themselves ZSTs.

Guarantee that enums with Rust representation with a single struct-like variant with no fields, or where all fields are ZSTs, are themselves ZSTs.

@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label May 5, 2026
@DanielEScherzer DanielEScherzer changed the title New rule: items.struct.zst New rules for ZSTs May 5, 2026
@traviscross traviscross changed the title New rules for ZSTs New rules for structs with no fields May 5, 2026
@traviscross traviscross changed the title New rules for structs with no fields Structs with no fields are ZSTs May 5, 2026
@traviscross traviscross changed the title Structs with no fields are ZSTs Structs with no fields or all-ZST fields are ZSTs May 5, 2026
@traviscross traviscross added I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. T-lang Relevant to the language team. labels May 5, 2026
@traviscross
Copy link
Copy Markdown
Contributor

cc @RalfJung @rust-lang/opsem

Comment thread src/type-layout.md
For [structs] with no fields, or where all fields are [zero-sized], it is further guaranteed that the structs are themselves [zero-sized].

r[layout.repr.rust.enum-struct-like-zst]
For [enums] (without a primitive representation specified) with a single struct-like variant with no fields or where all fields are [zero-sized], the enum itself is [zero-sized].
Copy link
Copy Markdown
Contributor

@theemathas theemathas May 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems bizarre to guarantee this for struct-like variants but not unit-like variants or tuple-like variants.

View changes since the review

@RalfJung
Copy link
Copy Markdown
Member

RalfJung commented May 6, 2026

That seems reasonable to me (specifically for repr(Rust); not necessarily for other reprs). Someone should check whether it is honored by -Zrandomize-layout.

@traviscross
Copy link
Copy Markdown
Contributor

traviscross commented May 6, 2026

We talked about this in the lang call today. Let's propose to do this with the correction that, for enums, we mean those repr(Rust) enums without a primitive representation specified and that have a single field-struct-like variant, a single unit-struct-like variant, or a single tuple-struct-like variant where the struct-like thing has no fields or where all of the fields are zero-sized.

@rfcbot fcp merge lang

@rust-rfcbot
Copy link
Copy Markdown
Collaborator

rust-rfcbot commented May 6, 2026

Team member @traviscross has proposed to merge this. The next step is review by the rest of the tagged team members:

Concerns:

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@traviscross
Copy link
Copy Markdown
Contributor

@rfcbot concern check-randomize-layout

@tmandry
Copy link
Copy Markdown
Member

tmandry commented May 6, 2026

This seems stronger than what we guarantee for non-ZST structs, which require #[repr(transparent)] to have the same representation as the inner type. In the lang meeting, @steffahn pointed out that the non-ZST case is harder because certain types get passed in registers, etc.

I don't see a reason not to make this guarantee, and it seems like a useful one, so let's do it.

@rfcbot reviewed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. proposed-final-comment-period S-waiting-on-review Status: The marked PR is awaiting review from a maintainer T-lang Relevant to the language team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants